package xyz.yuzegod.megawalls.classes.normal.cow;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("抚慰之哞", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 1.7;
            }
            case 2: {
                return 1.9;
            }
            case 3: {
                return 2.1;
            }
            case 4: {
                return 2.3;
            }
            case 5: {
                return 2.5;
            }
            default: {
                return 1.7;
            }
        }
    }
    
    public int getRegenLevel(final int level) {
        switch (level) {
            case 1:
            case 2: {
                return 1;
            }
            case 3:
            case 4:
            case 5: {
                return 2;
            }
            default: {
                return 1;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7哞,");
            lore.add("   §7自身获得抗性提升I和生命");
            lore.add("   §7恢复I效果,周围的队友获得");
            lore.add("   §7持续§a" + this.getAttribute(level) + "§7秒的生命恢复§a" + StringUtils.level(this.getRegenLevel(level)) + "§7效果。");
            return lore;
        }
        lore.add(" §8▪ §7哞,");
        lore.add("   §7自身获得抗性提升I和生命");
        lore.add("   §7恢复I效果,周围的队友获得");
        lore.add("   §7持续§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7秒的生命恢复§8" + StringUtils.level(this.getRegenLevel(level - 1)) + " ➜ §a" + StringUtils.level(this.getRegenLevel(level)) + "§7效果。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final int level = kitStats.getSkillLevel();
        final Player player = gamePlayer.getPlayer();
        if (player.hasPotionEffect(PotionEffectType.DAMAGE_RESISTANCE)) {
            player.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
        }
        player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, (int)(this.getAttribute(level) * 20.0), 0));
        if (player.hasPotionEffect(PotionEffectType.REGENERATION)) {
            player.removePotionEffect(PotionEffectType.REGENERATION);
        }
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, (int)(this.getAttribute(level) * 20.0), 0));
        for (final Player teammate : this.getTeammates(player, 5)) {
            if (teammate.hasPotionEffect(PotionEffectType.REGENERATION)) {
                teammate.removePotionEffect(PotionEffectType.REGENERATION);
            }
            teammate.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, (int)(this.getAttribute(level) * 20.0), this.getRegenLevel(level)));
        }
        new BukkitRunnable() {
            int step = 0;
            
            public void run() {
                ++this.step;
                if (this.step == 40 || !gamePlayer.isOnline()) {
                    this.cancel();
                    return;
                }
                ParticleEffect.HEART.display(0.2f, 0.5f, 0.2f, 1.0f, 1, gamePlayer.getPlayer().getLocation().add(0.0, 1.0, 0.0), 30.0);
            }
        }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        return true;
    }
    
    private List<Player> getTeammates(final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (!GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
